class Base::UsersController < ApplicationController
  before_action :check_permission, only: [:edit,:new,:index]
  before_action :set_base_user, only: [:show, :edit, :update, :destroy]

  # GET /base/users
  # GET /base/users.json
  def index
    @html_title = '用户列表'
    @base_users = Base::User.this.includes(:department).page(params[:page]||1)
  end

  # GET /base/users/1
  # GET /base/users/1.json
  def show
    render layout: 'modal'
  end

  # GET /base/users/new
  def new
    @html_title = '新加用户'
    @base_user = Base::User.new
    render layout: 'modal'
  end

  # GET /base/users/1/edit
  def edit
    render layout: 'modal'
  end

  # POST /base/users
  # POST /base/users.json
  def create
    @base_user = Base::User.this.new(base_user_params)
    @base_user.active = true
    if @base_user.save
      show_js_notice'创建成功！'
    else
      error_notice @base_user
    end
  end

  # PATCH/PUT /base/users/1
  # PATCH/PUT /base/users/1.json
  def update
    if @base_user.update(base_user_params)
      show_js_notice '修改成功！'
      # format.html { show_js_notice '删除成功！'  }
      # format.json { render :show, status: :ok, location: @base_user }
    else
      error_notice @base_user
    end
  end

  # DELETE /base/users/1
  # DELETE /base/users/1.json
  def destroy
    @base_user.resign!
    respond_to do |format|
      format.html { show_js_notice '删除成功！' }
      format.json { head :no_content }
    end
  end

  private
    def check_permission
      unless %w[xyy joel peter].include?(current_user.username)
        return show_js_notice '权限不足！'
      end
    end

    # Use callbacks to share common setup or constraints between actions.
    def set_base_user
      @base_user = Base::User.this.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def base_user_params
      params.require(:base_user).permit(:name_cn, :gender, :username, :password, :active, :branch_office_id, :department_id, :email, :mobile, :last_login_at, :leave_time, :phone_ext)
    end
end
